#!/bin/sh

revision="zlib v0.1.3 (2023-06-21 by MIBonk, many thanks to MatiasO)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

OPTION=$1

if [ -z "$1" ]; then
	echo "No option selected"
	OPTION="--help"
fi

if [ ! -f "$2" ]; then
	echo "Input FILE $2 does not exist"
	OPTION="--help"
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

case $OPTION in

-d|-decompress) {

	trap '' 2
	TEMP=$(echo $2 | $SED 's/.[^.][^.]*$//')
	echo -ne "Uncompressing...\n" | $TEE -a $LOG
	[ -z "$GEM" ] && echo -ne "\n"
	echo -ne " $2 to $TEMP.bin\n\n" >> $LOG
	echo -ne 1F8B080000000000 | $XXD -r -p > $TMP/zlib_header
	cat $TMP/zlib_header $2 | $GZIP -fdc > $TEMP.bin 2>/dev/null
	rm -rf $TMP/zlib_header
	trap 2

	return 2> /dev/null

};;

-c|-compress) {

	trap '' 2
	echo -ne "Compressing..." | $TEE -a $LOG
	[ -z "$GEM" ] && echo -ne "\n"
	echo -ne " $2 " >> $LOG
	TEMP=$(echo $2 | $SED 's/.[^.][^.]*$//')
	a=1
	b=0
	count=0
	LENGTH=$(ls -ln $2 | awk '{print $5}')
	echo -ne "Generate checksum, this can take some time."
	while [ $count -lt $LENGTH ]; do
		[ -z "$GEM" ] && echo -n "."
		BYTE="$(cat $2 | $XXD -p -u -l 1 -s $count)"
		BYTE=$((16#$BYTE))
		a=$(($BYTE+$a))
		a=$(($a % 65521))
		b=$(($b+$a))
		b=$(($b % 65521))
		count=$(($count+1))
	done
	CHECKSUM=$(echo "ibase=10; obase=16; ($b * 65536) + $a" | $BC)
	if [ ${#CHECKSUM} -le 7 ]; then
		CHECKSUM="0"$CHECKSUM
	fi
	$GZIP -fn $2 >> $LOG # -k 
	LENGTH=$(ls -ln $2.gz | awk '{print $5}')
	LENGTH=$(($LENGTH-8))
	$DD bs=1 skip=8 count=$(($LENGTH-8)) if=$2.gz of=$TEMP.z 2>/dev/null
	echo 789c | $XXD -r -p -o 0 - $TEMP.z
	LENGTH=$(ls -ln $TEMP.z| awk '{print $5}')
	echo $CHECKSUM | $XXD -r -p -o 0 -seek $LENGTH - $TEMP.z
	rm -rf $2.gz
	[ -z "$GEM" ] && echo -ne "\n\n"
	trap 2

	return 2> /dev/null

};;


        # help or unknown parameter ------------------------------
        *) {
                echo ""
                echo $revision
                echo ""
                echo "Usage: "$thisname" [OPTION]"
                echo ""
                echo "Options:"
                echo "        -d, -decompress FILE	extract zlib file into a binary"
                echo "        -c, -compress   FILE	compress binary into .z file"
				echo "        --help			show this help"
                echo ""
                echo "This program is free software; you can redistribute it and/or"
                echo "modify it under the terms of the GNU General Public License"
                echo "as published by the Free Software Foundation; either version 2"
                echo "of the License, or (at your option) any later version."
                echo ""
                echo "This program is distributed in the hope that it will be useful,"
                echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
                echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
                echo "See the GNU General Public License for more details."
                echo ""
                echo "You should have received a copy of the GNU General Public License"
                echo "along with this program; if not, write to the Free Software Foundation,"
                echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
                echo ""

        };;

        esac

exit 0
